﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using Windvoice.Util;
using Windvoice.Album.Model;

namespace Windvoice.Album.DataAccess
{
    public class PhotoDAL
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["windvoice"].ToString();

        public int Photo_Insert(PhotoInfo photo)
        {
            string command = "INSERT INTO [Photo] ([title],[photo],[albumid],[uploadtime],[hits],[keywords]) VALUES('{0}','{1}',{2},'{3}',{4},'{5}')";
            command = String.Format(command,photo.ID,photo.Title,photo.Photo,photo.AlbumID,photo.UploadTime,photo.Hits,photo.Keywords);
            return AccessHelper.ExecuteNonQuery(ConnectionString,command);
        }

        public int Photo_Delete(int PhotoID)
        {
            string command = "DELETE FROM [Photo] WHERE [id]=" + PhotoID.ToString();
            return AccessHelper.ExecuteNonQuery(ConnectionString, command);
        }

        public int Photo_Update(PhotoInfo photo)
        {
            string command = "UPDATE [Photo] SET [title]='{0}',[photo]='{1}',[albumid]={2},[uploadtime]='{3}',[hits]={4},[keywords]={5} WHRER [id]={6}";
            return AccessHelper.ExecuteNonQuery(ConnectionString, command); 
        }

        public PhotoInfo Photo_Get(int PhotoID)
        {
            string command = "SELECT [id],[title],[photo],[albumid],[uploadtime],[hits],[keywords] FROM [Photo] WHERE [id]="+PhotoID.ToString();
            DataTable dt = AccessHelper.ExecuteDataSet(ConnectionString, command).Tables[0];
            if (dt.Rows.Count > 0) return new PhotoInfo(dt.Rows[0]);
            else return null;
        }

        public DataTable Photo_GetAll()
        {
            string command = "SELECT [id],[title],[photo],[albumid],[uploadtime],[hits],[keywords] FROM [Photo]";
            return AccessHelper.ExecuteDataSet(ConnectionString, command).Tables[0]; 
        }

        public DataTable Photo_GetList(int AlbumID)
        {
            string command = "SELECT [id],[title],[photo],[albumid],[uploadtime],[hits],[keywords] FROM [Photo] WHERE [albumid]="+AlbumID.ToString();
            return AccessHelper.ExecuteDataSet(ConnectionString, command).Tables[0];
        }


    }
}
